package com.youtui.manager.controller;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.youtui.common.constant.ResultMessage;
import com.youtui.common.util.DateUtils;
import com.youtui.common.util.ExcelImportUtil;
import com.youtui.common.util.NextIdUtil;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.entity.PredictIncomeEntity;
import com.youtui.manager.service.IExcelInputDataService;
import com.youtui.manager.service.IPredictIncomeService;

@Controller
@RequestMapping("/predict")
public class PredictIncomeController extends BaseController {

	@Autowired
	IPredictIncomeService predictIncomeService;

	@Autowired
	IExcelInputDataService excelInputDataService;

	@Autowired
	ExcelImportUtil excelImportUtil;

	@RequestMapping("/pagelist.do")
	@ResponseBody
	public ResultMessage findPageList(String appName, Date dealdate,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "20") Integer rows) {

		PageInfo<PredictIncomeEntity> pageinfo = predictIncomeService.findPredictIncomeEntityPageList(appName, dealdate,
				page, rows);
		return ResultUtil.result(ResultUtil.code_success, "开发者预收入列表", pageinfo);
	}

	@RequestMapping("/upload.do")
	@ResponseBody
	public ResultMessage upload(MultipartHttpServletRequest request) {
		// todo 导入理赔模板
		try {
			MultipartFile file = request.getFile("file");
			if (file == null) {
				return ResultUtil.result(ResultUtil.code_fail, "报表文件错误");
			}
			String fileName = file.getOriginalFilename();

			InputStream inputStream = file.getInputStream();
			// 获取excel对象
			Workbook workbook = excelImportUtil.getWorkBook(fileName, inputStream);
			// 获取第一个页
			Sheet sheet = workbook.getSheetAt(0);
			int lastRowNum = sheet.getLastRowNum();
			int successNum = 0;
			if (lastRowNum < 1) {
				return ResultUtil.result(ResultUtil.code_fail, "报表数据格式错误");
			}
			Long excId = NextIdUtil.getNextId();
			Date curdate = null;

			for (int i = 1; i <=lastRowNum; i++) {
				Row row = sheet.getRow(i);
				int cells = sheet.getRow(i).getPhysicalNumberOfCells();
				String[] s = excelImportUtil.getExcelRow(row, cells);
				if(s[0].indexOf("/")>0){
					s[0]=s[0].replaceAll("\\/", "-");
				}
				// s[0] 日期 s[1] appname s[2] appkey s[3]uv s[4] 预计收益 s[5] 可提现收益
				int result = predictIncomeService.insertPredictIncome(s, excId);
				successNum += result;
				if (curdate == null) {
					
					curdate = DateUtils.convert2Date(s[0], DateUtils.DF_SHORT_STR);
				}

			}

			excelInputDataService.insertExcelInputData(excId, fileName, curdate, lastRowNum, successNum, 1);

		} catch (Exception e) {
			e.printStackTrace();
			ResultUtil.result(ResultUtil.code_fail, "预收入报表上传出现异常");
		}
		return ResultUtil.result(ResultUtil.code_success, "预收入报表上传完成");
	}
	

	@RequestMapping("/export.do")
	@ResponseBody
	public void export(String appName, Date dealdate) throws Exception {
		List<PredictIncomeEntity> reportList = predictIncomeService.findPredictIncomeEntityList(appName, dealdate);
		/*for (int i = 0; i < reportList.size(); i++) {
			 
		}*/
		String fileName = "开发者预计收入报表" + DateUtils.getTimestamp();
		Map<String, Object> dataMap = new HashMap<String, Object>();
 
		excelUtil.excelData(request, response, fileName, "predict_excel", reportList, dataMap);
	}

}
